/**  
* Copyright (c) 2009, Noel Lynch All rights reserved. Redistribution and use  
* in source and binary forms, with or without modification, are permitted  
* provided that the following conditions are met: Redistributions of source  
* code must retain the above copyright notice, this list of conditions and the  
* following disclaimer. Redistributions in binary form must reproduce the above  
* copyright notice, this list of conditions and the following disclaimer in the  
* documentation and/or other materials provided with the distribution. Neither  
* the name of Noel Lynch nor the names of contributors may be used to  
* endorse or promote products derived from this software without specific prior  
* written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
*/
// created by noellynch
// Nov 2, 2009

package com.sceneworker.wizards;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.scene.Node;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.myjavautils.GenericUtils;
import com.myjavautils.swing.SimpleAlert;
import com.myjavautils.swing.SwingComponentFactory;

public class TexturedQuadWizard extends WizardBase {

	private		JTextField		mcl_quadName;
	private		JLabel			mcl_textureLocation;
	private		JCheckBox		mcl_createBlendState;
	
	public	TexturedQuadWizard(JDialog par) {
		super(par);
	}
	
	@Override
	public void doWizardUI(Object cl_treeParent) {
		setLayout(new MigLayout("inset 5"));
		
		final	WizardBase		lcl_this = this;
		
		JButton		lcl_locate = SwingComponentFactory.makeAButton("Locate Texture");
		add(lcl_locate);
		mcl_textureLocation = SwingComponentFactory.makeALabel("");
		add(mcl_textureLocation, "wrap");
		lcl_locate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				File		lcl_f = GenericUtils.getAnImageFile(null);
				if(lcl_f == null) { 
					return; 
				}
				
				mcl_textureLocation.setText(lcl_f.getPath());
				lcl_this.containerDialog.pack();
				lcl_this.containerDialog.setLocationRelativeTo(null);
			}
		});
		
		mcl_createBlendState = SwingComponentFactory.makeACheckBox("Create Blend State", true);
		add(mcl_createBlendState, "wrap");
	}
	
	@Override
	public	void	executeWizard(Object cl_treeParent) {
		if(!(cl_treeParent instanceof Node)) {
			SimpleAlert.doAlert("The selected object in the tree is not a Node!", this);
			return;
		}
		
		if(mcl_textureLocation.getText().equals("")) {
			SimpleAlert.doAlert("Texture location is invalid!", this);
			return;
		}
		
		Node		lcl_n = (Node)cl_treeParent;
		Quad		lcl_q = new Quad("quad" + lcl_n.getQuantity(), 10, 10);
		lcl_q.setModelBound(new BoundingBox());
		lcl_q.updateModelBound();
		
		Texture				lcl_t = TextureManager.loadTexture(mcl_textureLocation.getText(), Texture.MinificationFilter.Trilinear, 
				Texture.MagnificationFilter.Bilinear);
		TextureState		lcl_ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
		lcl_ts.setTexture(lcl_t);
		lcl_q.setRenderState(lcl_ts);
		
		if(mcl_createBlendState.isSelected()) {
			BlendState		lcl_bs = DisplaySystem.getDisplaySystem().getRenderer().createBlendState();
			lcl_bs.setBlendEnabled(true);
			lcl_q.setRenderState(lcl_bs);
		}
		
		lcl_n.attachChild(lcl_q);
		lcl_n.updateGeometricState(0, true);
		lcl_n.updateRenderState();
	}

}
